# Copyright 2010 Michael Penhallegon

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This module is for the views of the blog app
# TODO: move Page and Section into the blog app
from dj import Page, Section
from django.shortcuts import render_to_response
from django.template.loader  import get_template
from django.template import RequestContext
from django.views.decorators.csrf import csrf_protect

def blogrender(page):
    '''creates the context that is used for the most part of views function referring to the blog application'''
    return {'pages':Section.BaseData().getPages(),
            'articlelist':page.getArticle(),
            'css':page.getCSS(),
            'pagetitle':page.getTitle(),
            'link':page.getLink(),
            }
def blog(request,step=0):
   '''create the blog page of the website'''
   blogPage = Page.BlogPage(step)
   blogPage.blogrender()
   blogPage.sidebar()
   if step > 0:
       prevstep = blogPage.step-1
   else:
       prevstep = blogPage.step

   blogPage.context['step'] = {'next':blogPage.step+1,'prev':prevstep}
   return render_to_response('blog.html',blogPage.context)
def previewBlog(request,step=0):
   '''create the blog page of the website'''
   blogPage = Page.BlogPage(step)
   blogPage.blogrender()
   blogPage.sidebar()
   if step > 0:
       prevstep = blogPage.step-1
   else:
       prevstep = blogPage.step

   blogPage.context['step'] = {'next':blogPage.step+1,'prev':prevstep}
   return render_to_response('previewblog.html',blogPage.context)
def archive(request, year, month=0):
    '''retrieve list of articles from blog by year and/or month'''

    path = request.get_full_path().split('/')
    page = Section.BlogData()
    context = blogrender(page)
    return render_to_response('blog.html',context)

@csrf_protect
def article(request, title):
    '''create a page  to display a individual article'''
    path = request.get_full_path().split('/')
    page = Section.BlogData()
    context = blogrender(page)
    context['articlelist'] = page.getArticleBySlug(title)
    return render_to_response('comment.html',context,context_instance=RequestContext(request))

@csrf_protect
def preview(request, title):
    '''create the preview page that is used to preview a article'''
    path = request.get_full_path().split('/')
    page = Section.BlogData()
    context = blogrender(page)
    context['articlelist'] = page.getArticleBySlug(title.split('/')[0])
    if month > 0:
        context['articlelist'] = page.getArticleByDate(int(year),month=int(month))
    else:
        context['articlelist'] = page.getArticleByDate(int(year))

    return render_to_response('preview.html',context,context_instance=RequestContext(request))
